package com.ym.health.api.common;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.utils.StringUtil;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.domain.APIResponse;
import com.ym.medical.domain.vo.StandardDeptVo;
import com.ym.medical.service.ICommonSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author wenbo
 * @Date 2020/8/25 16:33
 **/
@Api(tags = "api-标准科室下拉")
@RestController
@RequestMapping(value = "/common/standard", headers = Constants.Api.VERSION)
@Validated
public class StandardDeptController {

    @Autowired
    private ICommonSV commonSV;

    @ApiOperation("标准科室父级列表")
    @GetMapping("/listPageParentStandept")
    @ResponseBody
    public APIResponse listPageStandept(
            @RequestParam(required = false, defaultValue = "1") int pageNo,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false, defaultValue = "1") String deptLevel) {
        String sql = "select id,dept_code,dept_name,dept_img_url,partent_dept_code,dept_level,dept_sort from standard_dept where dept_level = #{params.deptLevel} and deleted = '00' and state = '01' and dept_code != '-1'";
        HashMap<String, Object> params = new HashMap<>();
        params.put("deptLevel", deptLevel);
        PageDomain.startPage(pageNo,pageSize);
        List<StandardDeptVo> standardDeptVo = commonSV.queryListEntity(sql, params, StandardDeptVo.class);
        return APIResponse.success(new PageInfo<>(standardDeptVo));
    }


    @ApiOperation("标准科室子列表")
    @GetMapping("/listPageStandept")
    @ResponseBody
    public APIResponse<List<StandardDeptVo>> listPageStandept(
            @ApiParam(name = "deptCode", value = "父级科室编码", required = true)
            @RequestParam String deptCode) {
        HashMap<String, Object> params = new HashMap<>();
        String criteria = " ";
        if (StringUtil.isNotBlank(deptCode)) {
            params.put("deptCode", deptCode);
            criteria += "and partent_dept_code = #{params.deptCode}";
        }
        criteria += " order by dept_sort";
        String sql = "select id,dept_code,dept_name,dept_img_url,partent_dept_code,dept_level,dept_sort from standard_dept " +
                "where dept_level = 2 and deleted = '00' and state = '01' and dept_code != '-1'" + criteria;
        List<StandardDeptVo> standardDeptSimVo = commonSV.queryListEntity(sql, params, StandardDeptVo.class);
        return APIResponse.success(standardDeptSimVo);
    }
}
